	/**
	 *
	 * @author	Daniel Bergqvist 900203-2432
	 * @author	Marika Hansson 910305-0804
	 * @version second submission
	 * 
	 */
public class Uppg1 {

	private String strings[];
	private int nrElements = 0;
	private int p = 0;
	
	public Uppg1(int size){
		strings = new String[size];
	}
	
	public Uppg1(){
		strings = new String[20];
	}
	
	/**
	 * @param element to add
	 */
	public void addFirst(String element){
		addElement(element, 0);
	}
	
	private void addElement(String element , int position){
		if(element == null){
			throw new NullPointerException("the element is null");
		}
		
		if(nrElements < strings.length){		
			for(int i=nrElements; position<=i; i--){
				if(strings[i] != null){
					strings[i+1] = strings[i];
				}
			}
			strings[position] = element;
			nrElements++;
		}
		else{
			throw new IndexOutOfBoundsException("The list is full!");
		}
	}
	
	/**
	 * @return true if empty false otherwise.
	 */
	public boolean isEmpty(){
		return (nrElements == 0);
	}
	
	/**
	 * @return the string that's first in the array, null if array is empty
	 */
	public String getFirst(){
		if(isEmpty()){
			throw new NullPointerException("The list is empy");
		}
		return strings[0];
	}
	
	/**
	 * @return appropriate value depending on if the removal was successful or not.
	 */
	public boolean removeFirst(){
		if(isEmpty()){
			return false;
		}
		else{
			for(int i=0; i<nrElements-1; i++){
				strings[i] = strings[i+1];
			}
			nrElements--;
		}
		return true;
	}
	
	/**
	 * @param searchString the string to search for in the array.
	 * @return returns the index where the searchString is in the array, -1 if it doesn't exist.
	 */
	public int exist(String searchString){
		for(int i=0; i<nrElements; i++){
			if(strings[i] == searchString){
				return i;
			}
		}
		return -1;
	}
	
	
	/**
	 * @return elements in the array as a stringbuilder
	 */
	public String toString(){
		StringBuilder holder = new StringBuilder("[ ");
		int counter = 0;
		for(int i=0; i<strings.length; i++){
			if(strings[i] != null){
				holder.append(strings[i]);
				counter++;
				if(counter == nrElements){
					break;
				}
				holder.append(", ");
			}
		}
		holder.append(" ]");
		return holder.toString();
	}
	/**
	 * @param p, a pointer. 
	 */
	public void setP(int p){
		if(p>=0 && p<strings.length){
			this.p = p;
		}
		else{
			throw new IndexOutOfBoundsException("Array Index isnt so big, program terminate in setP(int p) method ");
		}
	}
	
	/**
	 * @return true if position p+1 have any element in strings array
	 */
	public boolean hasNext(){
		return (p+1 <= strings.length);
	}
	
	/**
	 * @param element to add after p
	 */
	public void addAfterP(String element){
		if(hasNext()){
			addElement(element, p);
		}
		else{
			throw new IndexOutOfBoundsException("The list is full!");
		}
	}
	
	/**
	 * @param i, a value that point on a position in strings array
	 * @return returns the element in strings array that i is pointing at.
	 */
	public String get(int i){
		if(i > nrElements || i < 0){
			throw new IndexOutOfBoundsException("Invalid index entered");
		}
		return strings[i];
	}
	
	/**
	 * @param p, increase or decrease the p pointer.
	 */
	public void moveP(int p){		
		if((this.p+p) <= strings.length && (this.p+p)>=0){
			this.p+=p;
		}
		else{
			throw new IndexOutOfBoundsException("Array Index isnt so big, program terminate in moveP(int p) method");
		}
	}
	
	/**
	 * @param element to which p should point
	 */
	public void setPtoElement(String element){
		setP(exist(element));
	}
}